package com.techshinogongjian.fp.util;

public class FpConst {
	
	
	/** =========API错误码==================== **/
	public static final int TCY_SUCC = 0;  // 成功操作
	public static final int TCY_FAIL = -1; // 失败结果
	public static final int TCY_ERRO = -2; // 校验错误
	public static final int TCY_PARA = -3; // 参数错误
	public static final int TCY_EMPT = -4; // 空特征库
	public static final int TCY_NOFP = -5; // 未按或孬
	public static final int TCY_NSAM = -6; // 值不相关
	public static final int TCY_NMAT = -7; // 值不匹配
	public static final int TCY_NMEM = -8; // 内存不足
	public static final int TCY_FLSH = -9; // 有闪存错
	public static final int TCY_NODV = -10; // 传感器错
	public static final int TCY_TOLV = -11; // 请抬起手
	public static final int TCY_NSUP = -12; // 不支持令
	public static final int TCY_TMOT = -13; // 操作超时
	public static final int TCY_BUSY = -14; // 我很忙啊
	public static final int TCY_NLNK = -15; // 设备断开
	public static final int TCY_LESS = -16; // 特点过少
	public static final int TCY_CNCL = -17; // 取消操作
	public static final int TCY_FILE = -18; // 文件错误

	public static final int DOG_NONE = -19; // 狗狗不在
	public static final int DOG_TMOT = -20; // 狗已老啦
	public static final int DOG_BIND = -21; // 狗非本属
	public static final int TCY_NOT_EXISTS = -22; // 标识不存在
	
	public static final int TCY_CAPTUREIMAGE = 1; // 读取指纹图像
	public static final int TCY_GETIMAGE = 4; // 出图
	public static final int TCY_ENROLL_START = 2; // 开始注册
	public static final int TCY_MOREIMAGE = 3; // 注册/比对 要求继续采集图像
	public static final int TCY_VERIFY_START = 6; // 开始比对

	
	/* ===================蓝牙设备指令======================= */
	public static final int COM_HDR = 0x7E; // 命令头

	public static final int LSR_BIN = 0x42; // 二进制传
	public static final int LSR_SPT = 0x53; // 字节拆分

	public static final int LSB_BIN = 0x62; // 蓝牙二进制传
	public static final int LSB_SPT = 0x73; // 蓝牙字节拆分

	public static final int LSR_SPEC = 0x00; // 专用指令

	public static final int LSR_INFO = 0x61; // 内核信息
	public static final int LSR_BAUD = 0x62; // 设波特率
	public static final int LSR_REGI = 0x63; // 注册指纹
	public static final int LSR_VERI = 0x64; // 库中比对
	public static final int LSR_FLSR = 0x65; // 读闪存区

	public static final int LSR_DITO = 0x67; // 访问数据
	public static final int LSR_LINK = 0x68; // 连接测试
	public static final int LSR_STEP = 0x69; // 特征分块
	public static final int LSR_HINT = 0x6A; // 友好提示
	public static final int LSR_FCFG = 0x6B; // 闪存配置
	public static final int LSR_RREG = 0x6C; // 读寄存器

	public static final int LSR_SCFG = 0x71; // 系统配置
	public static final int LSR_LOAD = 0x72; // 重新加载
	public static final int LSR_HARD = 0x73; // 强度测试
	public static final int LSR_CTRL = 0x74; // 背光蜂鸣
	public static final int LSR_RSTX = 0x75; // 硬件复位
	public static final int LSR_CBRT = 0x76; // 标定UPEK

	public static final int LSR_CAPT = 0x80; // 抓拍图像
	public static final int LSR_PIMG = 0x81; // 置图当前
	public static final int LSR_MNUT = 0x82; // 当前特征
	public static final int LSR_MTCH = 0x83; // 比对指纹

	public static final int LSR_RXGT = 0x91; // 超级注册

	public static final int LSR_OPAY = 0x92; // 液晶语音
	public static final int LSR_STRS = 0x93; // 显示串儿

	public static final int LSR_KBGN = 0x94; // 启动键盘
	public static final int LSR_KBUF = 0x95; // 键盘缓冲

	/* ===================蓝牙设备指令======================= */
	public static final int LSB_LINK = 0x00; // 连接测试 用于探测设备通讯是否正常。
	public static final int LSB_INFO = 0x01; // 获取设备信息 指纹、虹膜设备通用。

	public static final int LSB_WRSN = 0x06; // SN写入
	public static final int LSB_RDSN = 0x07; // SN读取
	public static final int LSB_CTRL = 0x0F; // LED、蜂鸣器控制
	public static final int LSB_SCFG = 0x13; // 读设备当前电量 蓝牙指纹等的电池供电设备。
	public static final int LSB_DITO = 0x14; // 修改蓝牙名称
	public static final int LSB_RREG = 0x15; // 修改蓝牙 PIN 码

	public static final int LSB_CAPT = 0x22; // 获取原始图像 指纹/虹膜图像
	public static final int LSB_MNUT = 0x23; // 获取特征数据 指纹/虹膜特征值

	/* ===== 设备错误码 ================================================== */
	public static final int LSE_SUCC = 0x00; // 成功操作
	public static final int LSE_FAIL = 0x01; // 失败结果
	public static final int LSE_ERRO = 0x02; // 校验错误
	public static final int LSE_TMOT = 0x03; // 操作超时
	public static final int LSE_NODV = 0x04; // 未连设备
	public static final int LSE_FLSH = 0x05; // 写闪存错
	public static final int LSE_NOFP = 0x06; // 未按或孬
	public static final int LSE_NOSM = 0x07; // 值不相关
	public static final int LSE_NOMT = 0x08; // 值不匹配
	public static final int LSE_EPLB = 0x09; // 库链为空
	public static final int LSE_TOLV = 0x0A; // 请抬起手
	public static final int LSE_NMEM = 0x0B; // 内存不足
	public static final int LSE_NSUP = 0x0C; // 不支持令
	public static final int LSE_LESS = 0x0D; // 特点过少
	public static final int LSE_CNCL = 0x0E; // 取消操作

	public static final int LSE_BUSY = 0xFF; // 真的忙啊

	
	/*=== HID的USB采集器相关指令 ====================================*/

	public static final int  HID_VRSN	= 0x0000;		/* :版本+28字节图像属性 */
	public static final int  HID_RIMG	= 0x0001;		/* :???x???字节指纹数据 */
	public static final int  HID_PRES	= 0x0002;		/* 当前是否有手指纹按捺 */
	public static final int  HID_CTRL	= 0x0003;		/* 固定Nx种用户友好提示 */
	public static final int  HID_BOOT	= 0x0004;		/* ARCA置BOOT，CDB=BoOt */
	public static final int  HID_RDSN	= 0x0005;		/* 读取设备厂商产品SN号 */
	public static final int  HID_WTSN	= 0x0006;		/* 写入设备厂商产品SN号 */
	public static final int  HID_RFLS	= 0x0007;		/* 读1K字节为单位的闪存 */
	public static final int  HID_WFLS	= 0x0008;		/* 写1K字节为单位的闪存 */
	public static final int  HID_RREG	= 0x0009;		/* 按索引R读4字节寄存器 */
	public static final int  HID_WREG	= 0x000A;		/* 按索引W写4字节寄存器 */
	public static final int  HID_TEST	= 0x000B;		/* 进入强度测试老化状态 */

	/*=== USB采集器相关指令 =========================================*/
	public static final int  CMD_LNKOK	= 0x00;	// rd	64字节			// 检测设备连接
	public static final int  CMD_RESET	= 0x01;	// wt	64字节			// 复位目标设备
	public static final int  CMD_GETSN	= 0x02;	// rd	64字节			// 获取设备SN值
	public static final int  CMD_SETSN	= 0x03;	// wt	64字节			// 设置设备SN值
	public static final int  CMD_RDIMG	= 0x04;	// rd	xx字节			// 读取指纹图像
	public static final int  CMD_RDFLS	= 0x05;	// rd	1K字节			// 读取闪存数据
	public static final int  CMD_WRFLS	= 0x06;	// wt	1K字节			// 写入闪存数据
	public static final int  CMD_BOOTL	= 0x07;	// wt	64字节			// Arca置BootLd
	public static final int  CMD_HINTS	= 0x08;	// --	00字节			// 用户友好提示
	public static final int  CMD_FPHDR	= 0x09;	// rd	28字节			// 获取图像属性
	public static final int  CMD_RDREG	= 0x52;	// rd	04字节			// 按索引读'R取
	public static final int  CMD_WTREG	= 0x57;	// wr	04字节			// 按索引写'W入

	public static final int  CMD_RFLMU	= 0x80F0;	// rd	读闪存特征		// 小端2-by索引
	public static final int  CMD_WFLMU	= 0x00F1;	// wr	写闪存特征		// 小端2-by索引
	public static final int  CMD_RSRMU	= 0x80F2;	// rd	读SRAM特征		// 1-by位置索引
	public static final int  CMD_WSRMU	= 0x00F3;	// wr	写SRAM特征		// 1-by位置索引
	public static final int  CMD_MSRMU	= 0x80F4;	// rd	设备内比对		// 2x1-by索引值
	public static final int  CMD_MAXLB	= 0x80F5;	// rd	最大库枚数		// 2x1-by枚数值
	public static final int  CMD_SUBCT	= 0x00F6;	// --	0蜂鸣1小灯		// 开关1x0.01秒

	public static final int  HRD_TEST	= 0x00F7;	// --	设备的强测		// 不返回PC数据
	public static final int  RD_DEVSN	= 0x80F8;	// rd	读取设备SN		// TESO自定32符
	public static final int  WR_DEVSN	= 0x00F9;	// wr	写入设备SN		// TESO自定32符
	public static final int  WR_FLSPG	= 0x00FA;	// wr	按1K页仅写		// 算法库更新用
	
	// 私有化SerialTool类的构造方法，不允许其他类生成SerialTool对象
	public FpConst() {
	}
	///////////////////////////////////////////////////////////////////////////////
	//
	//
	// 二代证指纹核验错误码
	//
	//
	///////////////////////////////////////////////////////////////////////////////
	public static final int MSG_SUCCESS = 0; // 成功
	public static final int MSG_FAILURE = 1; // 失败
	public static final int MSG_INVALID_VALUE = 2;// 数据非法
	public static final int MSG_ERROR = 4; // 错误
	public static final int MSG_ENROLL_START = 5;// 录入过程开始
	public static final int MSG_VERIFY_START = 6; // 开始比对
	public static final int MSG_BADIMAGE = 7; // 采集的图像质量太差
	public static final int MSG_MOREIMAGE = 8; // 注册要求继续采集图像
	public static final int MSG_NOTEXIST = 9; // 标识不存在
	public static final int MSG_CAPTUREIMAGE = 10; // 开始读取指纹图像
	public static final int MSG_DETECTIMAGE = 12; // 正在检测手指
	public static final int MSG_SENSOR_FAILURE = 11; // 采集仪故障
	public static final int MSG_NOT_SUPPORTED_PARAM = 19; // 不支持的参数
	public static final int MSG_MEMORY_ERROR = 20; // 模块内部存储出错
	public static final int RET_NO_CONNECTION = -100; // 模块没有连接
	public static final int RET_IMAGE_ERROR = -101; // 图像下载出错
	public static final int RET_TIMEOUT = -102; // 超时错误

	public static final int RET_CANCEL = -103;// 取消操作
	public static final int RET_LINK = -104; // 设备断开

	/* command code */
	public static final int CMD_NONE = 0;
	public static final int CMD_SETBAUD = 1;
	public static final int CMD_SETSLEVEL = 2;
	public static final int CMD_SETENROLL = 4;
	public static final int CMD_SETVERIFY = 5;
	public static final int CMD_SETTEMPLATE = 6;
	public static final int CMD_SETDELETE = 8;
	public static final int CMD_SETENROLLIMAGECOUNT = 12;

	public static final int CMD_QUERYTEMPLATE = 19;
	public static final int CMD_GETBAUD = 20;
	public static final int CMD_GETSLEVEL = 21;
	public static final int CMD_GETCOUNT = 24;
	public static final int CMD_GETTEMPLATE = 23;
	public static final int CMD_GETUID = 29;
	/* return code */
	public static final int RET_SETBAUD = 39;
	public static final int RET_GETBAUD = 40;
	public static final int RET_SETTEMPLATE = 41;
	public static final int RET_GETTEMPLATE = 42;
	public static final int RET_SETSLEVEL = 44;
	public static final int RET_SETDELETE = 45;
	public static final int RET_SETVERIFY = 46;
	public static final int RET_GETCOUNT = 49;
	public static final int RET_QUERYTEMPLATE = 50;
	public static final int RET_GETSLEVEL = 52;
	public static final int RET_IMAGEPARAM = 56;
	public static final int RET_IMAGELINE = 57;
	public static final int RET_SETENROLL = 47;
	public static final int RET_SETENROLLIMAGECOUNT = 64;
	public static final int RET_GETUID = 65;
	public static final int RET_GETVTEMPLATE = 80;

	public static byte crc8b(byte b, byte crc) {
		int i;
		crc ^= b;
		for (i = 0; i < 8; i++) {
			if ((crc & 0x01) != 0) {
				crc = (byte) (((crc & 0xff) >>> 1) ^ 0x8C);
			} else {
				crc = (byte) ((crc & 0xff) >>> 1);
			}
		}
		return crc;
	}

	public static byte crc8(byte[] ptr, int len) {
		byte crc;

		crc = 0x00;
		int i = 0;
		while (len-- > 0) {
			crc = crc8b(ptr[i++], crc);
		}
		return (byte) crc;
	}

	// 在指纹数据前添加2个字节
	public static byte[] ConvertID2(byte[] id2, int cFlag) {
		byte[] data;
		int i;
		byte k;

		data = new byte[514]; // 512+2
		System.arraycopy(id2, 0, data, 2, 512);
		FlipID2(data, cFlag);
		data[0] = 0x1; // The first added byte
		k = 0;
		for (i = 0; i < 512; i++) {
			k = (byte) (((k & 0xff) + (data[i + 2] & 0xff)) & 0xff);
		}
		data[1] = k; // the second added byte
		return data;
	}

	// 对指纹数据做镜像变换。cFlag=0：不做变换；(cFlag & 0x1)!=0 做横向镜像；(cFlag & 0x2)!=0 做纵向镜像
	public static int FlipID2(byte[] idb, int cFlag) {
		int i, count, x, y, d;
		int k = 2;

		if (cFlag == 0)
			return 1;
		count = idb[19 + 2];
		for (i = 0; i < count; i++) {
			k = 31 + i * 4;
			x = idb[k] & 0xff;
			y = (idb[k + 1] & 0xff) + ((idb[k + 3] & 0x1) & 0xff) * 256;
			d = (idb[k + 2] & 0xff)
					+ ((((idb[k + 3] & 0xff) >> 1) & 0x1) & 0xff) * 256;

			if ((cFlag & 0x1) != 0) { // H flipped (mirrored)
				x = 255 - x;
				d = 180 - d;
				if (d < 0)
					d += 360;
			}
			if (((cFlag >> 1) & 0x1) != 0) { // V flipped (mirrored)
				y = 359 - y;
				d = 360 - d;
				if (d < 0)
					d += 360;
			}
			idb[k] = (byte) x;
			idb[k + 1] = (byte) (y & 0xFF);
			idb[k + 2] = (byte) (d & 0xFF);
			idb[k + 3] = (byte) ((((d >> 8) & 0x1) << 1) | ((y >> 8) & 0x1) | 0xFC);
		}
		return 1;
	}

}